package us.icitap.cm.ui.policecase;

import java.util.Arrays;

import us.icitap.cm.CaseManagementUI;
import us.icitap.cm.business.AppListTypeEnum;
import us.icitap.cm.business.entities.AppEntity;
import us.icitap.cm.business.entities.AppList;
import us.icitap.cm.business.entities.EntityTypeEnum;
import us.icitap.cm.business.entities.Evidence;
import us.icitap.cm.business.entities.Evidence.EvidenceTypeEnum;
import us.icitap.cm.business.entities.Experiment;
import us.icitap.cm.business.entities.Experiment.ExperimentActionEnum;
import us.icitap.cm.listeners.AppEntityListener;
import us.icitap.cm.ui.custom.CustBeanFieldGroup;
import us.icitap.cm.ui.custom.CustComboBox;
import us.icitap.cm.ui.custom.CustDateField;
import us.icitap.cm.ui.custom.CustTextArea;
import us.icitap.cm.ui.custom.CustomFormLayout;
import us.icitap.cm.ui.shared.AppEntityLayout;
import us.icitap.cm.utils.ProjectLists;
import us.icitap.utils.FormatingUtils;

import com.vaadin.data.Property.ValueChangeEvent;
import com.vaadin.data.Property.ValueChangeListener;
import com.vaadin.data.fieldgroup.FieldGroup.CommitException;
import com.vaadin.data.util.BeanItemContainer;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Button.ClickListener;
import com.vaadin.ui.CheckBox;
import com.vaadin.ui.FormLayout;
import com.vaadin.ui.TabSheet;

public class ExperimentDataLayout extends AppEntityLayout{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 8224429998985961731L;


	private Experiment experiment;
	
	
	private final CustBeanFieldGroup<Experiment> binder = new CustBeanFieldGroup<Experiment>(Experiment.class);
	protected AppEntityListener listener;
	
	private final CheckBox evidenceCheckBox = new CheckBox("Eshte prove materiale");
	
	private final CustomFormLayout cfl = new CustomFormLayout("450px", "Sende te perfshire ne ngjarje", "Reset", "Ruaj");
	
	private final EvidenceEmbeddableDataLayout evidenceEmbDataLayout = new EvidenceEmbeddableDataLayout();
	
	final TabSheet tabSheet = new TabSheet();
	
	private final CustDateField expDateField = new CustDateField("Data e eksperimentit");
	private final CustComboBox expReasonSelect = new CustComboBox("Arsyeja e eksperimentit");
	private final  CustTextArea placeDoneTextArea = new CustTextArea("Vendi i realizimit");
	private final CustComboBox expActionSelect = new CustComboBox("Veprimet ne eksperiment");
	private final  CustTextArea resultTextArea = new CustTextArea("Rezultatet e eksperimentit");
	
	
	public ExperimentDataLayout() {
		this.setMargin(true);
		final TabSheet tabSheet = new TabSheet();
		
		FormLayout fl = new FormLayout();
		fl.addStyleName("cmLayout");
		fl.setMargin(true);
		fl.addComponent(this.expDateField);
		fl.addComponent(this.expReasonSelect);
		fl.addComponent(this.placeDoneTextArea);
		fl.addComponent(this.expActionSelect);
		fl.addComponent(this.resultTextArea);
		
		fl.addComponent(this.evidenceCheckBox);
		
		tabSheet.addTab(fl, "Te dhenat");
		
		
		
		cfl.addComponent(tabSheet);
		cfl.addFooter();
		resultTextArea.setWidth("20em");
		this.expDateField.setDateFormat(FormatingUtils.SHORT_DATE_FORMAT);
		
		expReasonSelect.setContainerDataSource(new BeanItemContainer<AppList>(AppList.class, ProjectLists.getList(AppListTypeEnum.ExperimentReason, false)));
		
		expActionSelect.setContainerDataSource(new BeanItemContainer<ExperimentActionEnum>(ExperimentActionEnum.class, Arrays.asList(ExperimentActionEnum.values())));

		
		cfl.addRightClickListener(new ClickListener() {
			
			/**
			 * 
			 */
			private static final long serialVersionUID = -595906231664940646L;

			@Override
			public void buttonClick(ClickEvent event) {
				try {
					binder.commit();
					if(ExperimentDataLayout.this.listener != null){
						if(oldAppEntity == null){
							// Eshte entitet i ri
							if((evidenceCheckBox.getValue().booleanValue())){
								experiment.getAppEntity().setEvidence(evidenceEmbDataLayout.getEvidence());	
							}
							ExperimentDataLayout.this.listener.addAppEntityToReport(experiment.getAppEntity());
						}
						else{
							//Eshte entitet ekzistues
							
							experiment.getAppEntity().setEvidence(evidenceEmbDataLayout.getEvidence());
							if(!oldAppEntity.myEquals(experiment.getAppEntity())){
								//Entiteti ka ndryshuar
								if(experiment.getAppEntity().getEvidence() != null && 
										experiment.getAppEntity().getEvidence().myEquals(oldAppEntity.getEvidence())){
									//Nuk ka ndryshuar prova
									experiment.getAppEntity().setEvidence(oldAppEntity.getEvidence());
								}
								
								experiment.getAppEntity().setRecordUser(CaseManagementUI.getUser().getUsername());
								ExperimentDataLayout.this.listener.updateAppEntityIntoReport(oldAppEntity, experiment.getAppEntity());
							}
							else
							{
								//Entiteti nuk ka ndryshuar, as prova
								ExperimentDataLayout.this.listener.refresh(oldAppEntity);
							}
							
							addAppEntity();
						}
							
	
						
					}
				} catch (CommitException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		});
		
		
		
		
		binder.bind(this.expDateField, "experimentDate");
		binder.bind(this.expReasonSelect, "experimentReason");
		binder.bind(this.placeDoneTextArea, "placeDone");
		binder.bind(this.expActionSelect, "experimentAction");
		binder.bind(this.resultTextArea, "result");
		
		evidenceCheckBox.setImmediate(true);
		evidenceCheckBox.addValueChangeListener(new ValueChangeListener() {
			
			/**
			 * 
			 */
			private static final long serialVersionUID = -3334992668114195148L;

			@Override
			public void valueChange(ValueChangeEvent event) {
				if(evidenceCheckBox.getValue().booleanValue()){
					// Entiti shoqerohet me prove
					tabSheet.addTab(evidenceEmbDataLayout, "Prove hetimore");
					
					if(oldAppEntity != null && oldAppEntity.getEvidence() != null){
						// Entiteti ekzistues ka prove ekzistuese te lidhur me te
						experiment.getAppEntity().setEvidence(new Evidence(oldAppEntity.getEvidence()));
						evidenceEmbDataLayout.loadEvidence(experiment.getAppEntity().getEvidence(),true);
					}
					else{
						// Entiteti ekzistues nuk ka prove ekzistues te lidhur me te
						// Krijohet prove e re 
						evidenceEmbDataLayout.createEvidence(EvidenceTypeEnum.MATERIAL);
					}
				}
				else{
					// Entiteti nuk ka prove ekzistuese te lidhur me te
					experiment.getAppEntity().setEvidence(null);
					tabSheet.removeComponent(evidenceEmbDataLayout);
					
				}
					
				
				
			}
		});
		
		this.addComponent(cfl);
	}
	
	
	@Override
	public void addAppEntity() {
		this.tabSheet.setSelectedTab(0);
		AppEntity appEntity = new AppEntity(CaseManagementUI.getUser().getUsername(), EntityTypeEnum.EXPERIMENT);
		experiment = appEntity.getExperiment();
		binder.setItemDataSource(experiment);
		
	}
	
	private AppEntity oldAppEntity;
	
	@Override
	public void loadAppEntity(AppEntity appEntity, boolean editable) {
		oldAppEntity = appEntity;
		
		AppEntity newAppEntity = new AppEntity(appEntity);
		
		this.experiment = newAppEntity.getExperiment();
		binder.setItemDataSource(experiment);
		
		
		if(newAppEntity.getEvidence()!= null){
			evidenceCheckBox.setValue(new Boolean(true));
			evidenceEmbDataLayout.loadEvidence(newAppEntity.getEvidence(), true);
			tabSheet.addTab(evidenceEmbDataLayout, "Prove hetimore");
		}
		else{
			evidenceCheckBox.setValue(new Boolean(false));
		}
		
	}
	@Override
	public void addAppEntityListener(AppEntityListener listener) {
		this.listener = listener;
		
	}

}
